//
//  UmengSoaiclTool.m
//  money_ios
//
//  Created by dzmmac on 15/7/6.
//  Copyright (c) 2015年 dzmmac. All rights reserved.
//

#import "ShareManager.h"
#import "AppConfig.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "WXApi.h"
#import "HudManager.h"
@implementation ShareManager

+ (ShareManager *)sharedManager
{
    static ShareManager *sharedManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    return sharedManagerInstance;
}

-(id)init
{
    self.shareView = [ShareView sharedManager];
    self.shareView.delegate = self;
    self.manager = [SDWebImageManager sharedManager];
    return [super init];
}

/**
 *  设置QQ和微信分享的Url
 *
 *  @param shareUrl 分享的URL连接
 */
-(void)umengShareUrl:(NSString *)shareUrl shareImage:(UIImage  *)shareImage
           shareText:(NSString *)shareText viewController:(UIViewController *)viewController   success:(UMSocialSuccessBlock)successBlock error:(UMSocialErrorBlock)errorBlock
{
    
    self.umSocialSuccessBlock = successBlock;
    self.umSocialErrorBlock   = errorBlock;
    self.viewController = viewController;
    self.shareUrl = shareUrl;
    self.shareImage = shareImage;
    self.shareText = shareText;
    [self.shareView show];
    
}

/**
 *  设置QQ和微信分享的Url
 *
 *  @param shareUrl 分享的URL连接
 */
-(void)umengShareUrl:(NSString *)shareUrl shareImageUrl:(NSString  *)shareImageUrl
           shareText:(NSString *)shareText viewController:(UIViewController *)viewController
             success:(UMSocialSuccessBlock)successBlock error:(UMSocialErrorBlock)errorBlock{
    self.umSocialSuccessBlock = successBlock;
    self.umSocialErrorBlock   = errorBlock;
    self.viewController = viewController;
    self.shareUrl = shareUrl;
    [HudManager showLoading];
    WEAKSELF
    [self.manager downloadImageWithURL:[NSURL URLWithString:shareImageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        [HudManager hide];
        if (image) {
            successBlock();
            weakSelf.shareImage = image;
            weakSelf.shareText = shareText;
            [weakSelf.shareView show];
        } else {
            errorBlock();
        }
    }];
}

-(void)btnClicked:(UIButton *)btn
{
    UMSocialData *soData = [[UMSocialData alloc] initWithIdentifier:@"UMSOCIAL" withTitle:@"分享"];
    soData.shareImage = self.shareImage;
    soData.shareText = self.shareText;
    
    // 设置微信的分享URL
    soData.extConfig.wechatSessionData.url = self.shareUrl;
    soData.extConfig.wechatTimelineData.url = self.shareUrl;
    soData.extConfig.wechatSessionData.title = @"";
    soData.extConfig.wechatTimelineData.title = self.shareText;
    
    // 设置微信的分享URL
    soData.extConfig.qqData.url = self.shareUrl;
    soData.extConfig.qqData.title = @" ";
    
    UMSocialControllerService *socialService = [[UMSocialControllerService alloc] initWithUMSocialData:soData];
    socialService.socialUIDelegate = self;
    
    // 设置分享内容，和回调对象
    [[UMSocialControllerService defaultControllerService] setShareText:self.shareText shareImage:self.shareImage socialUIDelegate:self];
    
    NSString *snsName = [[UMSocialSnsPlatformManager sharedInstance].allSnsValuesArray objectAtIndex:btn.tag];
    
    if ([snsName isEqualToString:@"qq"] && ![QQApiInterface isQQInstalled]) {
        [HudManager showFailure:@"请安装QQ客户端"];
    }else if (([snsName isEqualToString:@"wxtimeline"] || [snsName isEqualToString:@"wxsession"]) && ![WXApi isWXAppInstalled])
    {
         [HudManager showFailure:@"请安装微信客户端"];
    }else{
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
        snsPlatform.snsClickHandler(self.viewController,socialService,YES);
    }
}

// 下面得到分享完成的回调
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    // 分享成功
    if(response.responseCode == UMSResponseCodeSuccess){
      
        if (self.umSocialSuccessBlock) {
             self.umSocialSuccessBlock();
        }
    }
}

-(void)didFinishGetUMSocialDataResponse:(UMSocialResponseEntity *)response
{
    if (self.umSocialErrorBlock) {
        self.umSocialErrorBlock();
    }
}
@end
